package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.constants.TheConstant;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.COrderEntity;
import com.nfu.zlsp.dev.entity.UserEntity;
import com.nfu.zlsp.dev.service.COrderService;
import com.nfu.zlsp.dev.service.ConsignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/corder")
public class COrderController {
    @Autowired
    private COrderService cOrderService;

    @Autowired
    private ConsignService consignService;


    @RequestMapping("/updateOrderStateByid/{id}")
    public R getGoods(@PathVariable("id") String id){
        R r = cOrderService.updateOrderStateById(id);
        return r;
    }

    /**
     * 承运方完成一键拼单
     * @param params
     * @param session
     * @return
     */
    @Transactional
    @RequestMapping("/payCOrder")
    public R payCOrder(@RequestParam Map<String, Object> params, HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO_TRANSPORT);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("t_userid",user.getId());

        R r = cOrderService.savePayList(params);
        return r;

    }



    /**
     * 托运方 ‘订单管理数据获取’
     */
    @RequestMapping("/myOrderManagementConsign")
    public R queryOrderManagementConsignPage(@RequestParam Map<String, Object> params, HttpSession session){

        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("c_userid",user.getId());
        PageUtils page = cOrderService.queryOrderManagementConsignPage(params);
        page.setTotalCount(page.getList().size());
        int totalPage = page.getTotalCount()% page.getPageSize()==0?page.getTotalCount()/ page.getPageSize():page.getTotalCount()/ page.getPageSize()+1;
        page.setTotalPage(totalPage);
        return R.ok().put("page", page);
    }

    /**
     * 承运方 ‘我拼的单数据获取’
     */
    @RequestMapping("/myOrderTransport")
    public R queryMyOrderTransport(@RequestParam Map<String, Object> params, HttpSession session){

        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO_TRANSPORT);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("t_userid",user.getId());
        PageUtils page = cOrderService.queryMyOrderTransport(params);
        page.setTotalCount(page.getList().size());
        int totalPage = page.getTotalCount()% page.getPageSize()==0?page.getTotalCount()/ page.getPageSize():page.getTotalCount()/ page.getPageSize()+1;
        page.setTotalPage(totalPage);
        return R.ok().put("page", page);
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("dev:corder:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("dev:corder:info")
    public R info(@PathVariable("id") String id){
		COrderEntity cOrder = cOrderService.getById(id);

        return R.ok().put("cOrder", cOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("dev:corder:save")
    public R save(@RequestBody COrderEntity cOrder){
		cOrderService.save(cOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("dev:corder:update")
    public R update(@RequestBody COrderEntity cOrder){
		cOrderService.updateById(cOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("dev:corder:delete")
    public R delete(@RequestBody String[] ids){
		cOrderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
